"use strict";

var timeout;
app.controller("ServReqController", ["$scope", "$stateParams", "$modal", "$timeout", "RESTService",
    function ($scope, $stateParams, $modal, $timeout, RESTService) {
        $scope.like = $stateParams.like;
        $scope.page = {
            totalItems: 0,
            currentPage: 1,
            currentItems: 0,
            maxSize: 5,				//max size of pagination
            itemsPerPage: 10,
            like: ($stateParams.like == null ? "" : $stateParams.like)
        };
        $scope.setPage = function (pageNo) {
            $scope.page.currentPage = pageNo;
        };
        $scope.pageChanged = function () {
            loadServReqListData();
        };

        $scope.servReqs = [];
        var loadServReqListData = function () {
            if ($scope.userRole == null) {
                $scope.userRole = "all";
            }
            RESTService.getResponse("/rest/servreq/getServReqsCount?status=all&like=" + $scope.page.like).then(function (response) {
                $scope.page.totalItems = response[0].count;
            });
            RESTService.getResponse("/rest/servreq/getServReqs?status=all&like=" + $scope.page.like + "&pageNo=" + $scope.page.currentPage + "&pageSize=" + $scope.page.itemsPerPage)
                .then(function (response) {
                    $scope.servReqs = response;
                    $scope.page.currentItems = $scope.servReqs.length;
                });
        }
        $scope.init = function () {
            loadServReqListData();
        }

        //3 open modal
        $scope.open = function (attrs, model) {
            // model.scope = $scope;
            var modalInstance = $modal.open(angular.extend({
                backdrop: "static",
                controller: "ModalInstanceCtrl",
                resolve: {
                    items: function () {
                        return [Object(model)];
                    }
                },
                scope : $scope
            }, attrs));
        };

        // share case
        $scope.sharePatient = function(a, b){
            var patient_Id = a.patient_Id ? a.patient_Id : a.patient_Id2;
            var params = {
                patient_Id:patient_Id
                ,friendId:b
            }
            console.log(params);
            RESTService.postResponse('/rest/chat/createChatMessageSharingPatient?', params).then(
                function (suc) {
                    console.log(suc);
                });
        }

    }]);

// app.controller("ServReqFlowController", ["$scope", "$modal", "$timeout", "RESTService", "$stateParams",
//     function ($scope, $modal, $timeout, RESTService, $stateParams) {
//         if($scope.selectLang!='English'){
//             $scope.styleFix={
//                 'cursor':'default',
//                 'min-width': '54px'
//             }
//         }else{
//             $scope.styleFix={
//                 'cursor':'default',
//                 'min-width': '124px'
//             }
//         }
//
//         $scope.ServReq = angular.extend({}, $scope.selected.item);
//
//         $scope.patientPackage = "";
//
//         $scope.service_Id = $stateParams.service_Id;
//
//         //$scope.like = $stateParams.like;
//
//         var loadServFlowData = function () {
//             // fix parameter
//             if ($scope.ServReq.servReq_Id === undefined) {
//                 $scope.ServReq.servReq_Id = '';
//             }
//             if ($scope.ServReq.patient_Id === undefined) {
//                 $scope.ServReq.patient_Id = '';
//             }
//             if ($scope.ServReq.patCase_Id === undefined) {
//                 $scope.ServReq.patCase_Id = '';
//             }
//
//             // if no servReq_Id, read patient info
//             RESTService.getResponse("/rest/patient/getPatientPackage?patient_Id=" + $scope.ServReq.patient_Id +
//                 "&patCase_Id=" + $scope.ServReq.patCase_Id + "&servReq_Id=" + $scope.ServReq.servReq_Id)
//                 .then(function (response) {
//                     $scope.patientPackage = response[0];
//                     console.log('数据包',$scope.patientPackage);
//                 });
//             RESTService.getResponse("/rest/diseasetype/getDiseaseTypeList?").then(function (response) {
//                 $scope.diseaseType = response;
//             });
//         };
//
//         $scope.init = function () {
//             loadServFlowData();
//         };
//
//         // open upload image window
//         $scope.openShowImage = function (mediCase) {
//             var modalInstance = $modal.open({
//                 backdrop: "static",
//                 templateUrl: 'ShowImage.html',
//                 controller: 'ModalInstanceCtrl',
//                 size: 'lg',
//                 resolve: {
//                     items: function () {
//                         return mediCase;
//                     }
//                 }
//             });
//             modalInstance.result.then(function (userId) {
//                 //ok click
//             }, function () {
//                 //cancel
//             });
//         };
//
//         //3 open modal
//         $scope.open = function (attrs, model) {
//             //model.scope = $scope;
//             var modalInstance = $modal.open(angular.extend({
//                 backdrop: "static",
//                 controller: "ModalInstanceCtrl",
//                 resolve: {
//                     items: function () {
//                         return [Object(model)];
//                     }
//                 }
//             }, attrs));
//         };
//     }]);
